
const crud = {};

crud.del = function(url, params, success, fail, msg, cancel = null) {
  // eslint-disable-next-line
  let vm = GLOBAL.vbus
  vm.$confirm(msg || '此操作将永久删除该数据, 是否继续?', '删除提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    var ps = params;
    if (params instanceof String || params instanceof Number || typeof (params) === 'number' || typeof (params) === 'string') {
      ps = { id: params };
    }
    vm.$api[url](ps).then(resData => {
      if (resData > 0) {
        vm.$message({ type: 'success', message: '删除成功!' });
        if (success) {
          success();
        }
      } else {
        if (fail) {
          fail(resData);
        } else {
          vm.$message({ type: 'error', message: '删除失败!' });
        }
      }
    }).catch(error => {
      console.error(error);
      if (fail) {
        fail(error);
      } else {
        vm.$message({ type: 'error', message: '删除失败!' });
      }
    });
  }).catch(err => {
    console.error(err);
    cancel();
    // if (fail) {
    //   fail('cancel')
    // }
  });
};

crud.dels = function(url, params, success, fail, msg) {
  // eslint-disable-next-line
  let vm = GLOBAL.vbus
  vm.$confirm(msg || '您确定删除选中的数据吗?', '删除提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    var ps = params;
    if (params instanceof Array) {
      ps = { ids: params };
    }
    vm.$api[url](ps).then(resData => {
      vm.$message({ type: 'success', message: '删除成功!' });
      if (success) {
        success();
      } else {
        if (fail) {
          fail(resData);
        } else {
          vm.$message({ type: 'error', message: '删除失败!' });
        }
      }
    }).catch(error => {
      console.error(error);
      if (fail) {
        fail(error);
      } else {
        vm.$message({ type: 'error', message: '删除失败!' });
      }
    });
  }).catch(err => {
    console.error(err);
    // if (fail) {
    //   fail('cancel')
    // }
  });
};

crud.checkUnique = function(url, params, callback) {
  return new Promise((resolve, reject) => {
    // eslint-disable-next-line
    GLOBAL.vbus.$api[url](params).then(resData => {
      callback();
      resolve();
    }).catch(error => {
      console.error(error);
      callback(new Error(params.value + '已存在'));
      resolve();
    });
  });
};

crud.operation = function(op, url, params, success, fail, msg) {
  // eslint-disable-next-line
  let vm = GLOBAL.vbus
  vm.$confirm(msg || `您确定${op}选中的数据吗?`, `${op}提示`, {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    vm.$api[url](params).then(resData => {
      vm.$message({ type: 'success', message: `${op}成功!` });
      if (success) {
        success();
      } else {
        if (fail) {
          fail(resData);
        } else {
          vm.$message({ type: 'error', message: `${op}失败!` });
        }
      }
    }).catch(error => {
      console.error(error);
      if (fail) {
        fail(error);
      } else {
        vm.$message({ type: 'error', message: `${op}失败!` });
      }
    });
  }).catch(err => {
    console.error(err);
    // if (fail) {
    //   fail('cancel')
    // }
  });
};

export default crud;
